#delimit;
clear;
set more off;

global temp /Sastemp;
global path ~;
set mem 5000m;

/*================================================
 Program: levyregs.do
 Author:  Avi Ebenstein
 Created: October 2008
 Purpose: Evaluate impact of levy system on firm behavior
=================================================*/

****************************************************;
* Step 0 - Use county data and collapse by province ;
****************************************************;

use  ~/research/pollution/datafiles/census_data, clear;
gen provnum=substr(GB_00,1,2);
destring provnum, replace force;
collapse (mean) education (sum) pop (mean) minority_prc (mean) non_ag_prc, by(provnum); 
save ~/research/pollution/datafiles/temp/temp,replace;

****************************************************;
* Step 1 - Use county data with watersheds assigned ;
****************************************************;

use ~/research/pollution/dumping/levies_dumping_output_1992to2002.dta, replace;
destring provnum, replace;
capture drop _merge;

sort provnum;
merge provnum using ~/research/pollution/datafiles/temp/temp;
keep if _merge==1|_merge==3;

gen region=.;
replace region=1 if provnum==11;
replace region=1 if provnum==12;
replace region=1 if provnum==13;
replace region=1 if provnum==14;
replace region=1 if provnum==15;
replace region=1 if provnum==21;
replace region=0 if provnum==22;
replace region=0 if provnum==23;
replace region=0 if provnum==31;
replace region=0 if provnum==32;
replace region=0 if provnum==33;
replace region=1 if provnum==34;
replace region=0 if provnum==35;
replace region=0 if provnum==36;
replace region=1 if provnum==37;
replace region=1 if provnum==41;
replace region=0 if provnum==42;
replace region=0 if provnum==43;
replace region=0 if provnum==44;
replace region=0 if provnum==45;
replace region=0 if provnum==46;
replace region=0 if provnum==50;
replace region=0 if provnum==51;
replace region=0 if provnum==52;
replace region=0 if provnum==53;
replace region=0 if provnum==54;
replace region=1 if provnum==61;
replace region=1 if provnum==62;
replace region=1 if provnum==63;
replace region=1 if provnum==64;
replace region=1 if provnum==65;

* 4 weird observations - I censor instead of dropping but the results are only changed slightly;
list province year if efflevy_water>5 & efflevy_water~=.;
*drop if efflevy_water>5 & efflevy_water~=.;
summarize efflevy_water,detail;
gen top99=r(p99);
summarize efflevy_water,detail;
gen bottom99=r(p1);
replace efflevy_water=bottom99 if efflevy_water<bottom99 & efflevy_water~=.;
replace efflevy_water=top99 if efflevy_water>top99 & efflevy_water~=.;

  
table year region, c(mean efflevy_water);
gen lnefflevy_water=ln(efflevy_water);
gen refflevy_water=efflevy_water;

table year region, c(mean efflevy_water);

**************;
* Regressions ;
**************;

drop if efflevy_water==.|provnum==0;

drop if provnum==0;

gen ratio=total_dumping /gross_output;
gen ln_dumpratio=ln(ratio);
gen ln_total_dumping=ln(total_dumping);

/* Add up cleanup costs */

gen other=0;
replace other=equipment_value/5 if equipment_value~=.;
replace other=other+(investment/5) if investment~=.;


gen total_cleanup_cost = operating_expenses+other;
gen ln_total_cleanup_cost=ln(total_cleanup_cost);

table year, c(sum total_cleanup_cost sum operating_expenses sum other mean efflevy_water mean lnefflevy_water);


/*sum equipment_value operating_expenses total_cleanup_cost;*/

/* Add up amounts cleaned up by chemical */

egen total_cleanup_tonnage = rsum(*_cleanup);
gen ln_total_cleanup_tonnage=ln(total_cleanup_tonnage);

egen cleanup_alt=rsum(volitized_phenol_cleanup cyanide_cleanup petroleum_cleanup chemical_oxygen_cleanup);
gen ln_cleanup_alt=ln(cleanup_alt);
xi: reg ln_cleanup_alt efflevy_water i.year i.province [w=pop],cluster(province) robust;

***********;

save ~/pollution/datafiles/levy_data, replace;

tab year,summ(efflevy_water);

xi: reg ln_dumpratio lnefflevy_water i.year i.province [w=pop],cluster(province) robust;
outreg2 lnefflevy_water using ~/pollution/outfiles/levyregs.out,replace  se bdec(3) tdec(3);

xi: reg ln_total_cleanup_tonnage lnefflevy_water i.year i.province [w=pop],cluster(province) robust;
outreg2 lnefflevy_water using ~/pollution/outfiles/levyregs.out,append  se bdec(3) tdec(3);

xi: reg ln_total_cleanup_cost lnefflevy_water i.year i.province [w=pop],cluster(province) robust;
outreg2 lnefflevy_water using ~/pollution/outfiles/levyregs.out,append  se bdec(3) tdec(3);

type ~/pollution/outfiles/levyregs.out;


exit;

